;(function(w){
    function Sk4t(textareaObj){
        this.self=textareaObj;
    }
    Sk4t.prototype.addTab=function(e){
        e&&e.preventDefault();
        this._insertText('    ');
    }
    Sk4t.prototype.addLinesTab=function(e){
        e&&e.preventDefault();
        this._tabText(']');
    }
    Sk4t.prototype.rmLinesTab=function(e){
        e&&e.preventDefault();
        this._tabText('[');
    }
    Sk4t.prototype._insertText=function(insert){
        var self=this.self;
        var start=self.selectionStart;
        var end=self.selectionEnd;
        var value=self.value;
        self.value=value.substr(0,start)+insert+value.substr(start);
        self.selectionStart=self.selectionEnd=start+insert.length;
    }
    Sk4t.prototype._tabText=function(flag){
        var self=this.self;
        var start=self.selectionStart;
        var end=self.selectionEnd;
        var value=self.value;
        var select=value.slice(start,end-1);
        var target;
        if(flag==']'){
            target=select.replace(/\n/g,'\n    ');
            if(start-1>0&&/\n/.test(value[start-1])){
                target='    '+target;
            }
        }
        if(flag=='['){
            target=select.replace(/\n    /g,'\n');
            if(start-1>0&&/\n/.test(value[start-1])){
                target=target.replace(/^    /,'');
            }
        }
        self.value=value.replace(select,target);
        self.selectionEnd=start+target.length+1;
        self.selectionStart=start;
    }
    w.sk4t=function(textareaObj){
        return new Sk4t(textareaObj)
    }
})(window)